package org.mycub.server.parser;

import org.json.JSONException;
import org.json.JSONObject;
import org.mycub.server.data.models.PaidParking;
import org.mycub.server.datainserter.PaidParkingsInserter;
import org.mycub.server.datainserter.factory.PaidParkingsInserterFactory;
import org.opengis.feature.simple.SimpleFeature;



public class CollectPaidParking extends CollectItem{

	PaidParking parking;

	public CollectPaidParking() {
		this.setPaidParking(new PaidParking());
	}

	public void collect(JSONObject json) {
		
		PaidParking parking = new PaidParking();
		
		try {
			String id = json.get("cle").toString();
			String nom = json.get("nom").toString();
			String nbPlaces = json.get("nombre_de_places").toString();
			String lon = json.get("x_long").toString();
			String lat = json.get("y_lat").toString();
			
			parking.setId(id);
			parking.setName(nom);
			parking.setPlaces((nbPlaces.isEmpty())?0:Integer.valueOf(nbPlaces));
			parking.setLongitude((lon.isEmpty())?0:Double.valueOf(lon));
			parking.setLatitude((lat.isEmpty())?0:Double.valueOf(lat));
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		System.out.println("collected paid parking : " + parking.getName() + 
				" id = " + parking.getId());

		savePaidParking(parking);
	}
	
	public void savePaidParking(PaidParking paidParkingCollected){
		PaidParkingsInserter paidParkingsInserter = new PaidParkingsInserterFactory().createPaidParkingsInserter();
		paidParkingsInserter.save(paidParkingCollected);
	}

	public PaidParking getPaidParking() {
		return parking;
	}

	public void setPaidParking(PaidParking parking) {
		this.parking = parking;
	}

	@Override
	public void collect(SimpleFeature feature) {

	}	
}
